﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using System.Globalization;

namespace PBDesk.Framework.LogNExpMgmt.Logging
{
    public class LogHelper
    {
        #region Constants
        
        public const string TRACE_LOGGER = "TraceLogger";
        public const string ERROR_LOGGER = "ErrorLogger";
        public const string STAT_LOGGER = "StatLogger";

        #endregion

        #region GetLogger Methods
        
        public static NLog.Logger GetLogger(LogType type)
        {
            NLog.Logger logger = null;
            switch(type)
            {
                case LogType.Trace:
                    {
                        logger = LogManager.GetLogger(TRACE_LOGGER);                        
                        break;
                    }
                case LogType.Stat:
                    {
                        logger = LogManager.GetLogger(STAT_LOGGER);
                        break;
                    }
                case LogType.Error:
                    {
                        logger = LogManager.GetLogger(ERROR_LOGGER);
                        break;
                    }              
            }
            return logger;
        }

        #endregion

        #region WriteLog Methods

        public static void WriteLog(LevelOfLog level, LogType type, string message)
        {
            NLog.Logger logger = GetLogger(type);
            if (logger != null)
            {
                switch (level)
                {
                    case LevelOfLog.Trace:
                        {
                            logger.Trace(message);
                            break;
                        }
                    case LevelOfLog.Error:
                        {
                            logger.Error(message);
                            break;
                        }
                    case LevelOfLog.Fatal:
                        {
                            logger.Fatal(message);
                            break;
                        }
                    case LevelOfLog.Info:
                        {
                            logger.Info(message);
                            break;
                        }

                }
            }
        }

        #endregion

        #region Exception Message Formatter

        internal static string FormatException(Exception ex)
        {
            return FormatException(string.Empty, ex);
        }
        internal static string FormatException(string message, Exception ex)
        {
            string returnStr = string.Empty;
            returnStr = string.Format(CultureInfo.InvariantCulture,
                                            "\n{0}\n{1}\n{2}\n",
                                            message ?? string.Empty,
                                            ex.Message,
                                            ex.StackTrace);

            if (ex.InnerException != null)
                returnStr = FormatException(returnStr, ex.InnerException);

            return returnStr;
        }

        #endregion
    }
}
